package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.java.controller;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Controller implements CodeGenTemplate {

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.javaModuleImplControllerFilePath();
    }

    @Override
    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code.apAndBl("package " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".controller." + localBinding.getSceneEnum().getBasePackage() + "." + table.getBusinessName() + ";");
        code.bl();
        code.apAndBl("import cn.iocoder.yudao.framework.common.genId.PKGenerator;");
        code.apAndBl("import org.springframework.web.bind.annotation.*;");
        code.apAndBl("import javax.annotation.Resource;");
        code.apAndBl("import org.springframework.validation.annotation.Validated;");
        if (localBinding.getSceneEnum().getScene() == 1) {
            code.apAndBl("import org.springframework.security.access.prepost.PreAuthorize;");
        }
        code.bl();
        code.apAndBl("import io.swagger.annotations.*;");
        code.bl();
        code.apAndBl("import javax.validation.constraints.*;");
        code.apAndBl("import javax.validation.*;");
        code.apAndBl("import javax.servlet.http.*;");
        code.apAndBl("import java.util.*;");
        code.apAndBl("import java.io.IOException;");
        code.bl();
        code.apAndBl("import " + globalBinding.getPageResultClassName() + ";");
        code.apAndBl("import " + globalBinding.getCommonResultClassName() + ";");
        code.apAndBl("import static " + globalBinding.getCommonResultClassName() + ".success;");
        code.bl();
        code.apAndBl("import " + globalBinding.getExcelUtilsClassName() + ";");
        code.bl();
        code.apAndBl("import " + globalBinding.getOperateLogClassName() + ";");
        code.apAndBl("import static " + globalBinding.getOperateTypeEnumClassName() + ".*;");
        code.bl();
        code.apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".controller." + localBinding.getSceneEnum().getBasePackage() + "." + table.getBusinessName() + ".vo.*;");
        code.apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".dal.dataobject." + table.getBusinessName() + "." + table.getClassName() + "DO;");
        code.apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".convert." + table.getBusinessName() + "." + table.getClassName() + "Convert;");
        code.apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".service." + table.getBusinessName() + "." + table.getClassName() + "Service;");
        code.bl();
        code.apAndBl("@Api(tags = \"" + localBinding.getSceneEnum().getName() + " - " + table.getClassComment() + "\")");
        code.apAndBl("@RestController");
        // 二级的 businessName 暂时不算在 HTTP 路径上，可以根据需要写
        code.apAndBl("@RequestMapping(\"/" + table.getModuleName() + "/" + localBinding.getSimpleClassName_strikeCase() + "\")");
        code.apAndBl("@Validated");
        code.apAndBl("public class " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "Controller {");
        code.bl();
        code.apAndBl("    @Resource");

        code.apAndBl("    private " + table.getClassName() + "Service " + localBinding.getClassNameVar() + "Service;");
        code.bl();
        code.apAndBl("    @PostMapping(\"/create\")");
        code.apAndBl("    @ApiOperation(\"创建" + table.getClassComment() + "\")");
        if (localBinding.getSceneEnum().getScene() == 1) {
            code.apAndBl("    @PreAuthorize(\"@ss.hasPermission('" + localBinding.getPermissionPrefix() + ":create')\")");
        }
        code.apAndBl("    public CommonResult<" + primaryColumn.getJavaType() + "> create" + localBinding.getSimpleClassName() + "(@Valid @RequestBody " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "CreateReqVO createReqVO) {");
        code.apAndBl("        createReqVO.setId(PKGenerator.newId());");
        code.apAndBl("        return success(" + localBinding.getClassNameVar() + "Service.create" + localBinding.getSimpleClassName() + "(createReqVO));");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @PutMapping(\"/update\")");
        code.apAndBl("    @ApiOperation(\"更新" + table.getClassComment() + "\")");
        if (localBinding.getSceneEnum().getScene() == 1) {
            code.apAndBl("    @PreAuthorize(\"@ss.hasPermission('" + localBinding.getPermissionPrefix() + ":update')\")");
        }
        code.apAndBl("    public CommonResult<Boolean> update" + localBinding.getSimpleClassName() + "(@Valid @RequestBody " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "UpdateReqVO updateReqVO) {");
        code.apAndBl("        " + localBinding.getClassNameVar() + "Service.update" + localBinding.getSimpleClassName() + "(updateReqVO);");
        code.apAndBl("        return success(true);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @DeleteMapping(\"/delete\")");
        code.apAndBl("    @ApiOperation(\"删除" + table.getClassComment() + "\")");
        code.apAndBl("    @ApiImplicitParam(name = \"id\", value = \"编号\", required = true, dataTypeClass = " + primaryColumn.getJavaType() + ".class)");
        if (localBinding.getSceneEnum().getScene() == 1) {
            code.apAndBl("    @PreAuthorize(\"@ss.hasPermission('" + localBinding.getPermissionPrefix() + ":delete')\")");
        }
        code.apAndBl("    public CommonResult<Boolean> delete" + localBinding.getSimpleClassName() + "(@RequestParam(\"id\") " + primaryColumn.getJavaType() + " id) {");
        code.apAndBl("        " + localBinding.getClassNameVar() + "Service.delete" + localBinding.getSimpleClassName() + "(id);");
        code.apAndBl("        return success(true);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @GetMapping(\"/get\")");
        code.apAndBl("    @ApiOperation(\"获得" + table.getClassComment() + "\")");
        code.apAndBl("    @ApiImplicitParam(name = \"id\", value = \"编号\", required = true, example = \"1024\", dataTypeClass = " + primaryColumn.getJavaType() + ".class)");
        if (localBinding.getSceneEnum().getScene() == 1) {
            code.apAndBl("    @PreAuthorize(\"@ss.hasPermission('" + localBinding.getPermissionPrefix() + ":query')\")");
        }
        code.apAndBl("    public CommonResult<" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "RespVO> get" + localBinding.getSimpleClassName() + "(@RequestParam(\"id\") " + primaryColumn.getJavaType() + " id) {");
        code.apAndBl("        " + table.getClassName() + "DO " + localBinding.getClassNameVar() + " = " + localBinding.getClassNameVar() + "Service.get" + localBinding.getSimpleClassName() + "(id);");
        code.apAndBl("        return success(" + table.getClassName() + "Convert.INSTANCE.convert(" + localBinding.getClassNameVar() + "));");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @GetMapping(\"/list\")");
        code.apAndBl("    @ApiOperation(\"获得" + table.getClassComment() + "列表\")");
        code.apAndBl("    @ApiImplicitParam(name = \"ids\", value = \"编号列表\", required = true, example = \"1024,2048\", dataTypeClass = List.class)");
        if (localBinding.getSceneEnum().getScene() == 1) {
            code.apAndBl("    @PreAuthorize(\"@ss.hasPermission('" + localBinding.getPermissionPrefix() + ":query')\")");
        }
        code.apAndBl("    public CommonResult<List<" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "RespVO>> get" + localBinding.getSimpleClassName() + "List(@RequestParam(\"ids\") Collection<" + primaryColumn.getJavaType() + "> ids) {");
        code.apAndBl("        List<" + table.getClassName() + "DO> list = " + localBinding.getClassNameVar() + "Service.get" + localBinding.getSimpleClassName() + "List(ids);");
        code.apAndBl("        return success(" + table.getClassName() + "Convert.INSTANCE.convertList(list));");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @GetMapping(\"/page\")");
        code.apAndBl("    @ApiOperation(\"获得" + table.getClassComment() + "分页\")");
        if (localBinding.getSceneEnum().getScene() == 1) {
            code.apAndBl("    @PreAuthorize(\"@ss.hasPermission('" + localBinding.getPermissionPrefix() + ":query')\")");
        }
        code.apAndBl("    public CommonResult<PageResult<" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "RespVO>> get" + localBinding.getSimpleClassName() + "Page(@Valid " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "PageReqVO pageVO) {");
        code.apAndBl("        PageResult<" + table.getClassName() + "DO> pageResult = " + localBinding.getClassNameVar() + "Service.get" + localBinding.getSimpleClassName() + "Page(pageVO);");
        code.apAndBl("        return success(" + table.getClassName() + "Convert.INSTANCE.convertPage(pageResult));");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("    @GetMapping(\"/export-excel\")");
        code.apAndBl("    @ApiOperation(\"导出" + table.getClassComment() + " Excel\")");
        if (localBinding.getSceneEnum().getScene() == 1) {
            code.apAndBl("    @PreAuthorize(\"@ss.hasPermission('" + localBinding.getPermissionPrefix() + ":export')\")");
        }
        code.apAndBl("    @OperateLog(type = EXPORT)");
        code.apAndBl("    public void export" + localBinding.getSimpleClassName() + "Excel(@Valid " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "ExportReqVO exportReqVO,");
        code.apAndBl("              HttpServletResponse response) throws IOException {");
        code.apAndBl("        List<" + table.getClassName() + "DO> list = " + localBinding.getClassNameVar() + "Service.get" + localBinding.getSimpleClassName() + "List(exportReqVO);");
        code.apAndBl("        // 导出 Excel");
        code.apAndBl("        List<" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "ExcelVO> datas = " + table.getClassName() + "Convert.INSTANCE.convertList02(list);");
        code.apAndBl("        ExcelUtils.write(response, \"" + table.getClassComment() + ".xls\", \"数据\", " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "ExcelVO.class, datas);");
        code.apAndBl("    }");
        code.bl();
        code.apAndBl("}");

        return code.toString();
    }

}